package com.cjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjh.mapper.EmpMapper;
import com.cjh.pojo.Emp;
import com.cjh.pojo.EmpSearch;
import com.cjh.service.EmpService;
import com.cjh.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    @Transactional(readOnly = true)
    public List<Emp> selectList(QueryWrapper<Emp> queryWrapper) {
        return empMapper.selectList(queryWrapper);
      }

    @Override
    @Transactional(readOnly = true)
    public IPage<Emp> selectPage(Page<Emp> page, QueryWrapper<Emp> queryWrapper) {
        return empMapper.selectPage(page, queryWrapper);
    }

    @Override
    @Transactional(readOnly = true)
    public Emp selectById(Integer id) {
        Emp redisEmp = (Emp) redisUtil.get("emp:" + id);
        if(Objects.equals(redisEmp, null)){
            Emp emp = empMapper.selectById(id);
            logger.info("===selectById（）缓存没有Emp数据，从数据库查询===");
            redisUtil.set("emp:" + id, emp);
            logger.info("===selectById（）数据库查询后将Emp放入redis===");
             return emp;
         }else{
            logger.info("===selectById（），缓存有Emp数据，从redis查询==="+redisEmp);
            return redisEmp;
         }
    }

    /**
     * 多条件查询
     * @param empSearch 员工信息关键字模糊查询
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<Emp> searchEmp(EmpSearch empSearch) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(!ObjectUtils.isEmpty(empSearch.getEmpName()), Emp.EMP_NAME, empSearch.getEmpName());

        queryWrapper.eq(!ObjectUtils.isEmpty(empSearch.getGender()), Emp.GENDER, empSearch.getGender());

        queryWrapper.eq(!ObjectUtils.isEmpty(empSearch.getEmail()), Emp.EMAIL, empSearch.getEmail());

        return empMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Emp> linkedSelect(Page<Emp> page) {

        return empMapper.linkedSelect(page);
    }

    @Override
    public Emp insert(Emp emp) {
        empMapper.insert(emp);
        return emp;
    }

    @Override
    @Transactional()
    public Integer deleteOneOrBatch(List id) {
        QueryWrapper<Emp> qu = new QueryWrapper<>();
        qu.in(!ObjectUtils.isEmpty(id),Emp.EMP_ID,id);
         return empMapper.delete(qu);
    }

    @Override
    public Emp updateById(Emp emp) {
        empMapper.updateById(emp);
        return emp;
    }
}
